﻿using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Windows;
using CAION2.Classes;
using CAION2.Commands;
using CAION2.Models;
using FirstFloor.ModernUI.Windows.Controls;

namespace CAION2.ViewModels
{
    public class ManastonesDialogViewModel : ViewModelBase
    {
        #region Vars
        
        private byte _maxLevel;
        private byte _currentLevel;
        private ManaStone _selectedManaStone;
        private ManaStone _currentManaStone;
        private ObservableCollection<ManaStone> _manastones;
        private ObservableCollection<Visibility> _levelVisible; 
        private string _parameter;
        private ManaStone.ManaStoneType _type;
        private byte _index;

        #endregion
        
        #region Properties

        public ModernDialog Dialog { private get; set; }

        public byte MaxLevel
        {
            set
            {
                if (Equals(_maxLevel, value)) return;
                _maxLevel = value;

                OnPropertyChanged("MaxLevel");

                UpdateLevelVisible();
            }
        }

        private byte CurrentLevel
        {
            get { return _currentLevel; }
            set
            {
                if (Equals(_currentLevel, value)) return;
                _currentLevel = value;

                OnPropertyChanged("CurrentLevel");

                UpdateManastonesList();
            }
        }

        public ManaStone SelectedManaStone
        {
            get { return _selectedManaStone; }
            set
            {
                if (Equals(_selectedManaStone, value)) return;
                _selectedManaStone = value;

                OnPropertyChanged("SelectedManaStone");

                if (_selectedManaStone != null) CurrentManaStone = SelectedManaStone;

                
            }
        }

        public ManaStone CurrentManaStone
        {
            get { return _currentManaStone; }
            set
            {
                if (Equals(_currentManaStone, value)) return;
                _currentManaStone = value;

                OnPropertyChanged("CurrentManaStone");

                CheckActionsManaStone();
            }
        }
        
        public ObservableCollection<ManaStone> Manastones
        {
            get { return _manastones; }
            set
            {
                if (Equals(_manastones, value)) return;
                _manastones = value;

                OnPropertyChanged("Manastones");
            }
        }

        public ManaStone.ManaStoneType Type
        {
            private get { return _type; }
            set
            {
                if (Equals(_type, value)) return;
                _type = value;

                OnPropertyChanged("Type");
            }
        }

        public byte Index
        {
            get { return _index; }
            set
            {
                if (Equals(_index, value)) return;
                _index = value;
            }
        }

        public ObservableCollection<Visibility> LevelVisible
        {
            get { return _levelVisible; }
            set
            {
                if (Equals(_levelVisible, value)) return;
                _levelVisible = value;

                OnPropertyChanged("LevelVisible");
            }
        }
        
        #endregion

        #region Commands

        public RelayCommand<string> ShowManastonesOfTypeCommand { get; private set; }
        public RelayCommand<string> ShowManastonesOfLevelCommand { get; private set; }
        public RelayCommand DeleteManaStoneCommand { get; private set; }
        public RelayCommand CopyManaStoneCommand { get; private set; }
        
        #endregion
        
        public ManastonesDialogViewModel()
        {
            ShowManastonesOfTypeCommand = new RelayCommand<string>(ShowManastonesOfType) { IsEnabled = true };
            ShowManastonesOfLevelCommand = new RelayCommand<string>(ShowManastonesOfLevel) { IsEnabled = true };
            DeleteManaStoneCommand = new RelayCommand(DeleteManaStone);
            CopyManaStoneCommand = new RelayCommand(CopyManaStone);

            LevelVisible = new ObservableCollection<Visibility>
                {
                    Visibility.Collapsed,
                    Visibility.Collapsed,
                    Visibility.Collapsed,
                    Visibility.Collapsed,
                    Visibility.Collapsed,
                    Visibility.Collapsed
                };
        }

        private void DeleteManaStone()
        {
            CurrentManaStone = new ManaStone(Type, Index);
            Dialog.Close();
        }

        private void CopyManaStone()
        {
            Dialog.Tag = "CopyManaStone";
            Dialog.Close();
        }

        private void ShowManastonesOfType(string parameter)
        {
            _parameter = parameter;
            if (CurrentLevel == 0) CurrentLevel = _maxLevel;

            UpdateManastonesList();
        }

        private void ShowManastonesOfLevel(string level)
        {
            CurrentLevel = Convert.ToByte(level);
            UpdateManastonesList();
        }

        private void UpdateManastonesList()
        {
            Manastones = new ObservableCollection<ManaStone>();

            if (string.IsNullOrEmpty(_parameter)) return;

            string sign = "=";
            if (CurrentLevel == 20) sign = "<=";

            string typeFilter = " NOT LIKE 'special_%'";
            if (Type == ManaStone.ManaStoneType.Special)
            {
                typeFilter = " LIKE 'special_%'";
            }

            DataTable manastonesTable = DataModel.GetTable("MagicStones", "*", "(stat_enchant_type1 LIKE '%" + _parameter + "%' OR "
                                                                         + "stat_enchant_type2 LIKE '%" + _parameter + "%' OR "
                                                                         + "stat_enchant_type3 LIKE '%" + _parameter + "%' OR "
                                                                         + "stat_enchant_type4 LIKE '%" + _parameter + "%' OR "
                                                                         + "stat_enchant_type5 LIKE '%" + _parameter + "%' )"
                                                                         + " AND level " + sign + CurrentLevel 
                                                                         + " AND name " + typeFilter + " ORDER BY id");
            foreach (DataRow row in manastonesTable.Rows)
            {
                Manastones.Add(new ManaStone(row, Type));
            }
        }

        private void CheckActionsManaStone()
        {
            DeleteManaStoneCommand.IsEnabled = CopyManaStoneCommand.IsEnabled = CurrentManaStone != null && CurrentManaStone.Row != null;
        }
        
        private void UpdateLevelVisible()
        {
            if (_maxLevel > 10) LevelVisible[0] = Visibility.Visible;
            if (_maxLevel > 20) LevelVisible[1] = Visibility.Visible;
            if (_maxLevel > 30) LevelVisible[2] = Visibility.Visible;
            if (_maxLevel > 40) LevelVisible[3] = Visibility.Visible;
            if (_maxLevel > 50) LevelVisible[4] = Visibility.Visible;
            if (_maxLevel > 60) LevelVisible[5] = Visibility.Visible;
        }
    }
    
}
